package io.renren.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.bean.ExercisesQuestionInfo;
import io.renren.dao.QuestionRecordDao;
import io.renren.entity.QuestionRecordEntity;
import io.renren.util.ExercisesUtil;
import io.renren.service.QuestionRecordService;
import io.renren.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 顺序练习
 * @author huolikang
 */
@Service("questionRecordService")
public class QuestionRecordServiceImpl extends ServiceImpl<QuestionRecordDao, QuestionRecordEntity> implements QuestionRecordService {

    @Autowired
    private QuestionService questionService;


    /**
     * 获取顺序练习序号
     * @param subjectId 科目id
     * @param questionId 问题id
     * @return
     */
    @Override
    public Integer getSort(Integer subjectId, Integer questionId) {
     List<Map<String,Object>>getSort=baseMapper.getSort(subjectId, questionId);
        if (getSort != null && getSort.size() > 0) {
            return getSort.size();
        } else {
            return 1;
        }

    }

    /***
     * 获取顺序练习题目
     * @param studentId 学员id
     * @param subjectId 科目id
     * @param sumQuestionNumber 总题数
     * @param sort 序号
     * @param state 是否用序号
     * @return
     */
    @Override
    public ExercisesQuestionInfo getSequenceQuestion(Long studentId, Integer subjectId, Integer sumQuestionNumber, Integer sort, Integer state) {
        //用做limit的运算
        Integer sort2=sort-1;
        //获取顺序练习题目
        List<Map<String, Object>> questionList = baseMapper.getQuestionList(studentId, subjectId, sort,sort2);
        //用于如果用户需要查询序号
        List<Map<String, Object>> menuSortInfoList = new ArrayList<Map<String, Object>>();
        if (state != null && state == 1) {
            menuSortInfoList = baseMapper.getMenuSortInfoList(studentId, subjectId);
        }
        // answerNumber 当前已答题数量
        Integer answerNumber = 0;
        //已答题数量
        List<Map<String, Object>> answerList = baseMapper.getAnswerList(subjectId, studentId);
        answerNumber = answerList == null ? 0 : answerList.size();
        return ExercisesUtil.checkQuestion(1, questionList, ExercisesUtil
                .checkSort(menuSortInfoList, sumQuestionNumber, answerNumber));
    }

    /***
     * 重置顺序练习
     * @param studentId 学员id
     * @param subjectId 科目id
     * @return R
     */
    @Override
    public void reset(Long studentId, Integer subjectId) {
        baseMapper.reset(studentId, subjectId);
    }

    /***
     * 保存顺序练习答案
     * @param studentId 学员id
     * @param questionId 问题id
     * @param studentAnswer 学生答案
     */
    @Override
    public void recordAnswer(Long studentId, Integer questionId, String studentAnswer) {
        QuestionRecordEntity questionRecordEntity = null;
        EntityWrapper<QuestionRecordEntity> entityEntityWrapper = new EntityWrapper<>();
        entityEntityWrapper.eq("question_id", questionId);
        entityEntityWrapper.eq("student_id", studentId);
        questionRecordEntity = this.selectOne(entityEntityWrapper);
        if (questionRecordEntity == null) {
            questionRecordEntity = new QuestionRecordEntity();
            questionRecordEntity.setLastModifyDate(new Date());
            questionRecordEntity.setCreateBy(studentId);
            questionRecordEntity.setCreateDate(new Date());
            questionRecordEntity.setStudentAnswer(studentAnswer);
            questionRecordEntity.setStudentId(studentId);
            questionRecordEntity.setQuestionId(questionId);
            insert(questionRecordEntity);
        }
    }
}
